package com.hengpeng.itfinoss.controller.stm;

import java.util.LinkedHashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.common.persist.entity.AbstractEntity.BoolValue;
import com.hengpeng.common.persist.query.model.Order.OrderType;
import com.hengpeng.common.scheduling.entity.SystemJob;
import com.hengpeng.common.scheduling.entity.SystemJob.SystemJobState;
import com.hengpeng.common.web.tags.ListTag.Pagination;
import com.hengpeng.itfinoss.controller.BaseController;
import com.hengpeng.itfinoss.service.ServiceFactory;
/**
 * <pre>
 * TODO 系统作业控制器
 * </pre>
 * @author pengfangliang
 * @version 1.0, 2010-9-19
 */
@Controller
@RequestMapping("/sjm")
public class SystemJobController extends BaseController
{
	@Autowired
	private ServiceFactory serviceFactory;

	@RequestMapping(value="/list")
	public String list(ModelMap map,HttpServletRequest request,String taskName, String desc, BoolValue canRun,String lastRunHostName, SystemJobState runState)
	{
		map.put("taskName", taskName);
		map.put("desc", desc);
		map.put("canRun", canRun);
		map.put("lastRunHostName", lastRunHostName);
		map.put("runState", runState);
		Pagination page = new Pagination(request);
		page.setPageSize(this.getPageSize(request));
		LinkedHashMap<String, OrderType> sortMap = new LinkedHashMap<String, OrderType>();
		sortMap.put("id", OrderType.DESC);
		try
		{
			List<SystemJob> systemJobList = serviceFactory.getSystemJobService().selectSystemJobPage(taskName, desc, canRun, lastRunHostName, runState, page.currentPage, page.pageSize, sortMap).getReturnValue();
			Long count = serviceFactory.getSystemJobService().selectSystemJobCount(taskName, desc, canRun, lastRunHostName, runState).getReturnValue();
			page.setEntityCount(count.intValue());
			map.put("systemJobList", systemJobList);
			map.put("systemJobListPage", page);
			return "/stm/systemjob/systemjob_list";
			
		}catch (Exception e)
		{
			e.printStackTrace();
			return returnError(map, e);
		}
	}
	
	@RequestMapping("/get/{id}")
	public String get(@PathVariable String className,ModelMap map)
	{
		if(className == null)
			return	returnError(map, "系统作业类名不能为空！");
		SystemJob sysjob = serviceFactory.getSystemJobService().getSystemJobByClassName(className);
		if(sysjob.getRunState().equals(SystemJobState.RUNING))
			return returnError(map, "作业正在运行中！");
		map.put("systemJob", sysjob);
		return "/stm/systemjob/modify_canRun";
	}
	
	@RequestMapping(value="/{className}",method=RequestMethod.POST)
	public String modify(@PathVariable  String className,BoolValue canRun,ModelMap map)
	{
		if(className == null)
			return	returnError(map, "要修改的系统作业不能为空！");
		try
		{
			SystemJob sysjob = serviceFactory.getSystemJobService().getSystemJobByClassName(className);
			if(sysjob.getRunState().equals(SystemJobState.RUNING))
				return returnError(map, "作业正在运行中！");
			sysjob.setCanRun(canRun);
			ServiceResult<SystemJob> result = serviceFactory.getSystemJobService().update(sysjob);
			return returnSuccess(map, result, "/sjm/list");
		}catch (Exception e)
		{
			e.printStackTrace();
			return returnError(map, e);
		}
	}
}